/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tabelamodelcolumnmodel.orcamento;

import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.AbstractCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.event.CellEditorListener;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;

/**
 *
 * @author icaro.klinger
 */
public class ButtonColumn extends AbstractCellEditor
        implements TableCellRenderer, TableCellEditor, MouseListener {

    private JTable jTable = null;
    private TableColumnModel columnModel = null;
    private int column;
    private String text = "ABC";
    private ImageIcon image = null;
    private JButton deleteButton = null;

    public ButtonColumn(JTable jTable, int column) {
        super();
        this.jTable = jTable;
        this.column = column;
        this.initialize();
    }

    public void initialize() {

        deleteButton = new JButton();
        deleteButton.setFocusPainted(false);

        this.columnModel = this.jTable.getColumnModel();
        this.columnModel.getColumn(column).setCellRenderer(this);
        this.columnModel.getColumn(column).setCellEditor(this);

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        JOptionPane.showMessageDialog(null, "FUI CLICADO");
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {

        if (hasFocus) {
            deleteButton.setForeground(table.getForeground());
            deleteButton.setBackground(UIManager.getColor("Button.background"));
        } else if (isSelected) {
            deleteButton.setForeground(table.getSelectionForeground());
            deleteButton.setBackground(table.getSelectionBackground());
        } else {
            deleteButton.setForeground(table.getForeground());
            deleteButton.setBackground(UIManager.getColor("Button.background"));
        }
        deleteButton.setText("TESTE");
        return deleteButton;
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column) {
        this.text = (value == null) ? "" : value.toString();
        deleteButton.setText(this.text);
        return deleteButton;
    }

    @Override
    public Object getCellEditorValue() {
        return text;
    }

    @Override
    public CellEditorListener[] getCellEditorListeners() {
        return super.getCellEditorListeners();
    }

    @Override
    public void addCellEditorListener(CellEditorListener arg0) {
        super.addCellEditorListener(arg0);
    }
    //Methods not Utilized

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }
   //End

//     JTable table;
//     JButton renderButton;
//     JButton editButton;
//     String text;
//
//     public ButtonColumn(JTable table, int column)
//     {
//         super();
//         this.table = table;
//         renderButton = new JButton();
//
//         editButton = new JButton();
//         editButton.setFocusPainted( false );
//         editButton.addActionListener( this );
//
//         TableColumnModel columnModel = table.getColumnModel();
//         columnModel.getColumn(column).setCellRenderer( this );
//         columnModel.getColumn(column).setCellEditor( this );
//     }
//
//    @Override
//     public Component getTableCellRendererComponent(
//         JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
//     {
//         if (hasFocus)
//         {
//             renderButton.setForeground(table.getForeground());
//             renderButton.setBackground(UIManager.getColor("Button.background"));
//         }
//         else if (isSelected)
//         {
//             renderButton.setForeground(table.getSelectionForeground());
//              renderButton.setBackground(table.getSelectionBackground());
//         }
//         else
//         {
//             renderButton.setForeground(table.getForeground());
//             renderButton.setBackground(UIManager.getColor("Button.background"));
//         }
//
//         renderButton.setText("Criar venda");
//         return renderButton;
//     }
//
//    @Override
//     public Component getTableCellEditorComponent(
//         JTable table, Object value, boolean isSelected, int row, int column)
//     {
//         //text = (value == null) ? "" : value.toString();
//         text =  "Criar venda";
//         editButton.setText( text );
//         return editButton;
//     }
//
//    @Override
//     public Object getCellEditorValue()
//     {
//         return text;
//     }
//
//    @Override
//     public void actionPerformed(ActionEvent e)
//     {
//         fireEditingStopped();
//         System.out.println( e.getActionCommand() + " : " + table.getSelectedRow());
//     }
}
